﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Office.Interop.Excel;

namespace WFL.Office.Excel
{
    public class ExcelWorkbooks : DisposableSite, IEnumerable<ExcelWorkbook>
    {
        public ExcelWorkbook this[int index]
        {
            get 
            {
                ExcelWorkbook item = new ExcelWorkbook(_workbooks[index]) { Application = Application };
                Trace(item);
                return item;
            }
        }

        public int Count
        {
            get { return _workbooks.Count; }
        }

        private Workbooks _workbooks;

        public ExcelApplication Application
        {
            get;
            internal set;
        }

        internal ExcelWorkbooks(Workbooks workbooks)
        {
            _workbooks = workbooks;
        }

        public ExcelWorkbook Open(string filename
            , UpdateLinksBehavior updateLinks = UpdateLinksBehavior.Prompt
            , bool readOnly = false
            , Delimiter? format = null
            , string password = null
            , string writeResPassword = null
            , bool ignoreReadOnlyRecommended = false
            , Platform? origin = null
            , string delimiter = null
            , bool editable = false
            , bool notify = false
            , object converter = null
            , bool addToMru = false
            , bool local = false
            , CorruptLoad corruptLoad = CorruptLoad.xlNormalLoad)
        {
            Workbook workbook = _workbooks.Open(filename
                , updateLinks
                , readOnly
                , format ?? Type.Missing
                , password ?? Type.Missing
                , writeResPassword ?? Type.Missing
                , ignoreReadOnlyRecommended
                , origin ?? Type.Missing
                , delimiter ?? Type.Missing
                , editable
                , notify
                , converter ?? Type.Missing
                , addToMru
                , local
                , corruptLoad);
            return new ExcelWorkbook(workbook);
        }

        public ExcelWorkbook Add(string template)
        {
            ExcelWorkbook item = new ExcelWorkbook(_workbooks.Add(template)) { Application = Application };
            Trace(item);
            return item;
        }

        public ExcelWorkbook Add(WBATemplate template = WBATemplate.xlWBATWorksheet)
        {
            ExcelWorkbook item = new ExcelWorkbook(_workbooks.Add(template)) { Application = Application };
            Trace(item);
            return item;
        }

        protected override void Dispose(bool disposing)
        {
            if (_workbooks != null)
            {
                if (disposing)
                {
                    Marshal.ReleaseComObject(_workbooks);
                }
                _workbooks = null;
                Application = null;
                base.Dispose(disposing);
            }
        }


        #region IEnumerable<ExcelWorkbook> 成员

        public IEnumerator<ExcelWorkbook> GetEnumerator()
        {
            foreach (Workbook workbook in _workbooks)
            {
                ExcelWorkbook item = new ExcelWorkbook(workbook);
                Trace(item);
                yield return item;
            }
        }

        #endregion

        #region IEnumerable 成员

        IEnumerator IEnumerable.GetEnumerator()
        {
            foreach (Workbook workbook in _workbooks)
            {
                ExcelWorkbook item = new ExcelWorkbook(workbook);
                Trace(item);
                yield return item;
            }
        }

        #endregion

    }
}
